package com.lijian.reptile.ui;

import com.lijian.reptile.model.Read;
import com.lijian.reptile.model.ThreadClass;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;

import static com.lijian.reptile.model.ThreadClass.startThread;

/**
 * ClassName: MyFrame
 * Description:
 * date: 2021/11/23 0023 下午 8:29
 *
 * @author LiJian
 * @since JDK 1.8
 */
public class MyFrame extends JFrame {

    //容器
    private JPanel jPanel = new JPanel();
    //文本域用来接收数据
    public static JTextArea jTextArea = new JTextArea();
    //文本域的滚动条  是一个滚动面版，只能添加一个组件
    private JScrollPane jsp_text = new JScrollPane(jTextArea);
    //url输入容器
    private JPanel jp_url = new JPanel();
    //网址输入框
    public static JTextField jtf_url = new JTextField("请输入网址！");
    //确认按钮
    private JButton bt_ascertain = new JButton("确定");
    //预览按钮
    private JButton bt_view = new JButton("预览");

    private ThreadClass threadClass;


    public MyFrame() {
        Read read = new Read();
        jPanel.add(jsp_text);
        //setEditable 文本域中的方法  是否可以编辑
        jTextArea.setEditable(true);
        //功能：自动换行
        jTextArea.setLineWrap(true);
        //设置文本域中滚动条大小
        jsp_text.setPreferredSize(new Dimension(550, 400));
        //在网址输入容器中添加
        jp_url.add(jtf_url);
        jp_url.add(bt_ascertain);
        jp_url.add(bt_view);

        jtf_url.setPreferredSize(new Dimension(200, 50));
        bt_ascertain.setPreferredSize(new Dimension(100, 50));
        bt_view.setPreferredSize(new Dimension(100, 50));

        jtf_url.addFocusListener(new FocusAdapter() {
            @Override
            public void focusGained(FocusEvent e) {
                jtf_url.setText("");
            }
        });
        bt_view.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new Perview();
            }
        });
        bt_ascertain.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                startThread(MyFrame.jtf_url.getText().trim());

                // MyFrame.jTextArea.append(MyFrame.jtf_url.getText());
            }
        });

        add(jPanel, BorderLayout.CENTER);
        add(jp_url, BorderLayout.SOUTH);
        setTitle("小说阅读器");
        setBounds(300, 100, 600, 500);
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);

    }

    public static void main(String[] args) {
        new MyFrame();

    }
}
